package persistency.reader;

/**
 * OfferedCourseReader.java
 * Purpose: this class represents a course that students can take
 * 
 * @author Hans Brems
 * @version 1.0 05/11/2011
 */

import java.util.ArrayList;

import model.Course;
import model.OfferedCourse;
import model.Teacher;

import model.container.CourseContainer;
import model.container.TeacherContainer;
import model.enums.Classroom;

import util.TimeBlock;
import util.enums.CourseDay;

public class OfferedCourseReader extends ContentReader {
	
	/** Constructs an offered course reader object */
	public OfferedCourseReader() {
	}
	
	/** Constructs an offered course reader object */
	public OfferedCourseReader(String filePath) {
		super(filePath);
	}
	
	/** Reads a text file containing offered course objects into a collection */
	public ArrayList<OfferedCourse> read() {
		
		CourseContainer courseContainer = CourseContainer.getInstance();
		TeacherContainer teacherContainer = TeacherContainer.getInstance();
		
		// Create list
		ArrayList<OfferedCourse> offeredCourses = new ArrayList<OfferedCourse>();
		
		// Skip the first line (header)
		this.scanner.nextLine();

		// Construct an offered course object from each line
		while (this.scanner.hasNext()) {
			
			
			
			// Create a new offered course
			OfferedCourse offeredCourse = new OfferedCourse();
			
			/* NOT YET READING ALL VALUES, LOOKING FOR A BETTER WAY TO DEAL WITH CLASSROOM
			 * CURRENTLY MISSING 5 FIELDS (Classroom, Day, Hour, Minute, Duration) */
			offeredCourse.setPrimaryKey(Integer.parseInt((this.scanner.next())));

			Course course = courseContainer.getCourseByID(Integer.parseInt(this.scanner.next()));
			offeredCourse.setCourse(course);
			course.getOfferedcourses().add(offeredCourse);
			int  classroomIndex= Integer.parseInt(this.scanner.next());
			try{
			offeredCourse.setClassroom(Classroom.values()[classroomIndex]);
			}catch(Exception e){
				e.printStackTrace();
			}
			Teacher teacher = teacherContainer.getTeacherByID(this.scanner.next());
			offeredCourse.setTeacher(teacher);
			teacher.getOfferedCourses().add(offeredCourse);
			offeredCourse.setNumberOfPlaces(Integer.parseInt(this.scanner.next()));
			String day = this.scanner.next();
			String hour = this.scanner.next();
			String minute = this.scanner.next();
			String duration = this.scanner.next();
			try {
				TimeBlock tb = new TimeBlock(CourseDay.valueOf(day), hour+":"+minute, Integer.parseInt(duration));
				offeredCourse.setTimeBlock(tb);
			} catch (NumberFormatException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
			
			
		
			
			// Add course to collection
			offeredCourses.add(offeredCourse);
		}
		
		// Return list
		return offeredCourses;
	}
}